package com.natasha.pins.data.dao;

import java.text.SimpleDateFormat;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.natasha.pins.data.bean.PinsMasterBean;
import com.natasha.pins.util.Constants;

public class PinsMasterDao {

	private SQLiteDatabase connection;
	private SimpleDateFormat dateTimeFormat;
	
	public PinsMasterDao(SQLiteDatabase connection){
		this.connection = connection;
		dateTimeFormat = new SimpleDateFormat(Constants.DATE_TIME_FORMAT);
	}
	
	public long createPin(PinsMasterBean pinsMasterBean){
		ContentValues values = new ContentValues();
			
		values.put(PinsMasterBean.NAME_PMUSER, pinsMasterBean.getPmuser());
		//values.put(PinsMasterBean.NAME_PMID, pinsMasterBean.getPmid());
		
		String dateString = dateTimeFormat.format(pinsMasterBean.getPmwhendt());
		values.put(PinsMasterBean.NAME_PMWHENDT, dateString);
		
		values.put(PinsMasterBean.NAME_PMTITLE, pinsMasterBean.getPmtitle());
		values.put(PinsMasterBean.NAME_PMDETAIL, pinsMasterBean.getPmdetail());
		values.put(PinsMasterBean.NAME_PMISFAV, pinsMasterBean.getPmisfav());
		values.put(PinsMasterBean.NAME_PMMEDIA, pinsMasterBean.getPmmedia());
		
		return connection.insert(PinsMasterBean.NAME_PINS_MASTER, null, values);
	}
	
	public Cursor fetchAllPins(){
		return connection.query(PinsMasterBean.NAME_PINS_MASTER, 
				new String[]{PinsMasterBean.NAME_PMUSER,PinsMasterBean.NAME_PMID,PinsMasterBean.NAME_PMWHENDT,PinsMasterBean.NAME_PMTITLE,
							PinsMasterBean.NAME_PMDETAIL,PinsMasterBean.NAME_PMISFAV,PinsMasterBean.NAME_PMMEDIA}, 
				null, null, null, null, null, null);
	}
	
	public Cursor fetchPin(long rowId) throws SQLException{
		Cursor cursor;
		
		cursor = connection.query(true, PinsMasterBean.NAME_PINS_MASTER, 
				new String[]{PinsMasterBean.NAME_PMUSER,PinsMasterBean.NAME_PMID,PinsMasterBean.NAME_PMWHENDT,PinsMasterBean.NAME_PMTITLE,
							PinsMasterBean.NAME_PMDETAIL,PinsMasterBean.NAME_PMISFAV,PinsMasterBean.NAME_PMMEDIA}, 
				PinsMasterBean.NAME_PMID+"="+rowId, 
				null, null, null, null, null);
		if(cursor != null){
			cursor.moveToFirst();
		}
		return cursor;
	}
	
	public long updatePin(PinsMasterBean newPin){
		ContentValues values = new ContentValues();
		
		String dateString = dateTimeFormat.format(newPin.getPmwhendt());
		values.put(PinsMasterBean.NAME_PMWHENDT, dateString);
		
		values.put(PinsMasterBean.NAME_PMTITLE, newPin.getPmtitle());
		values.put(PinsMasterBean.NAME_PMDETAIL, newPin.getPmdetail());
		values.put(PinsMasterBean.NAME_PMISFAV, newPin.getPmisfav());
		values.put(PinsMasterBean.NAME_PMMEDIA, newPin.getPmmedia());
		
		return connection.update(PinsMasterBean.NAME_PINS_MASTER, values, PinsMasterBean.NAME_PMID+"="+newPin.getPmid(), null);
	}
	
	public boolean deletePin( PinsMasterBean pinsMasterBean){
		return connection.delete(PinsMasterBean.NAME_PINS_MASTER, PinsMasterBean.NAME_PMID+"="+pinsMasterBean.getPmid(), null)>0;
	}
	
	public long deletePin(long id){
		return connection.delete(PinsMasterBean.NAME_PINS_MASTER, PinsMasterBean.NAME_PMID+"="+id, null);
	}
	
	public long deleteAllPins(){
		return connection.delete(PinsMasterBean.NAME_PINS_MASTER, "1=1", null);
	}
	
}
